/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Slave;

import Core.ConnectionHandler;
import Core.IConnectionHandler;
import Core.IConnectionListener;
import Core.MessageBuilder;
import Core.MessageType;
import Message.ChangeStatusMessage;
import Message.LogInMessage;
import Message.LogOutMessage;
import Message.SendBuzzMessage;
import Message.SendTextMessage;
import MessageHandler.IMessageHandler;
import java.io.IOException;
import java.net.Socket;
import org.openymsg.network.Session;

/**
 *
 * @author Mr Bao
 */
public class SlaveClient implements IConnectionListener {

    private Socket client;
    private SlaveServer slaveServer;
    private IConnectionHandler connectionHandler;
    private String host;
    private Session session;
    private SessionAdapter sessionAdapter;
    private boolean isSessionCreate = false;
    private long userId;

    public SlaveClient(SlaveServer slaveServer, Socket client, long userId) throws IOException {
        this.userId = userId;
        this.slaveServer = slaveServer;
        this.client = client;
        this.host = client.getInetAddress().getHostAddress();
        this.connectionHandler = new ConnectionHandler(client, this);
        ((ConnectionHandler) this.connectionHandler).SetDispatcher(this.slaveServer.GetDispatcher());
        ((ConnectionHandler) this.connectionHandler).Init();
    }

    public long GetUserId() {
        return userId;
    }

    public void IncommingMessage(String message) {
        if (!isSessionCreate) {
            session = new Session();
            sessionAdapter = new SessionAdapter((ConnectionHandler) this.connectionHandler);
            session.addSessionListener(sessionAdapter);
            isSessionCreate = true;
        }
        IMessageHandler messageHandler;
        if (message.startsWith(MessageType.Login)) {
            messageHandler = slaveServer.GetMessageHandler(LogInMessage.class);
            Object obj = messageHandler.Process(message, session);
            boolean isDone = Boolean.parseBoolean(obj.toString());
            String loginResultMessage;
            if (isDone) {
                loginResultMessage = MessageBuilder.BuildLogInResult(false, userId);
                ((ConnectionHandler) this.connectionHandler).QueueMessage(loginResultMessage);
            } else {
                loginResultMessage = MessageBuilder.BuildLogInResult(true, -1);
                ((ConnectionHandler) this.connectionHandler).QueueMessage(loginResultMessage);
                ((ConnectionHandler) this.connectionHandler).Shutdown(false);
                slaveServer.SingOff(this);
            }
        } else if (message.startsWith(MessageType.SendText)) {
            messageHandler = slaveServer.GetMessageHandler(SendTextMessage.class);
            messageHandler.Process(message, session);
        } else if (message.startsWith(MessageType.SendBuzz)) {
            messageHandler = slaveServer.GetMessageHandler(SendBuzzMessage.class);
            messageHandler.Process(message, session);
        } else if (message.startsWith(MessageType.ChangeStatus)) {
            messageHandler = slaveServer.GetMessageHandler(ChangeStatusMessage.class);
            messageHandler.Process(message, session);
        } else if (message.startsWith(MessageType.LogOut)) {
            messageHandler = slaveServer.GetMessageHandler(LogOutMessage.class);
            messageHandler.Process(message, session);
            slaveServer.SingOff(this);
        }
    }

    public void SocketClosed() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
